<?php

namespace Yunshop\VirtualCardTeam\common\models;

use app\common\models\BaseModel;
use Yunshop\VirtualCard\model\VirtualData;
use Yunshop\VirtualCard\model\VirtualType;

class GiveLog extends BaseModel
{

    protected $table = 'yz_virtual_card_team_card_log';
    protected $guarded = [''];
    protected $appends = ['type_name'];

    public function hasVirtualData()
    {
        return $this->hasOne(VirtualData::class, 'id', 'virtual_data_id');
    }

    public function member()
    {
        return $this->hasOne(\app\common\models\Member::class, 'uid', 'member_id');
    }

    public function order()
    {
        return $this->hasOne(\app\common\models\Order::class, 'id', 'order_id');
    }
    public function scopeSearch($query, $search)
    {
        $query->with([
            'hasVirtualData' => function ($query) {
                $query->select('id', 'name');
            },
        ]);



        if ($search['time'] && is_numeric($search['time'][0]) && is_numeric($search['time'][1])) {
            $query->whereBetween('created_at', [$search['time'][0]/1000, $search['time'][1]/1000]);
        }
    }
    public function getTypeNameAttribute()
    {
        return $this->attributes['type'] == 1?'下级购买商品':'后台发放';
    }
}